home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / Ellipses.frm (.txt) < prev    next >
Visual Basic Form  |  1999-03-18  |  5KB  |  160 lines

  1. VERSION 5.00
  2. Begin VB.Form frmEllipses 
  3.    Caption         =   "Ellipses"
  4.    ClientHeight    =   2550
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   7110
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   2550
  10.    ScaleWidth      =   7110
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.PictureBox picLineSegments 
  13.       Height          =   2295
  14.       Left            =   2400
  15.       ScaleHeight     =   2235
  16.       ScaleWidth      =   2235
  17.       TabIndex        =   3
  18.       Top             =   240
  19.       Width           =   2295
  20.    End
  21.    Begin VB.PictureBox picScaledCircle 
  22.       Height          =   2295
  23.       Left            =   4800
  24.       ScaleHeight     =   2235
  25.       ScaleWidth      =   2235
  26.       TabIndex        =   2
  27.       Top             =   240
  28.       Width           =   2295
  29.    End
  30.    Begin VB.PictureBox picCircle 
  31.       Height          =   2295
  32.       Left            =   0
  33.       ScaleHeight     =   2235
  34.       ScaleWidth      =   2235
  35.       TabIndex        =   0
  36.       Top             =   240
  37.       Width           =   2295
  38.    End
  39.    Begin VB.Label Label1 
  40.       Alignment       =   2  'Center
  41.       Caption         =   "Line Segments"
  42.       Height          =   255
  43.       Index           =   2
  44.       Left            =   2400
  45.       TabIndex        =   5
  46.       Top             =   0
  47.       Width           =   2295
  48.    End
  49.    Begin VB.Label Label1 
  50.       Alignment       =   2  'Center
  51.       Caption         =   "ScaledCircle"
  52.       Height          =   255
  53.       Index           =   1
  54.       Left            =   4800
  55.       TabIndex        =   4
  56.       Top             =   0
  57.       Width           =   2295
  58.    End
  59.    Begin VB.Label Label1 
  60.       Alignment       =   2  'Center
  61.       Caption         =   "Circle"
  62.       Height          =   255
  63.       Index           =   0
  64.       Left            =   0
  65.       TabIndex        =   1
  66.       Top             =   0
  67.       Width           =   2295
  68.    End
  69. Attribute VB_Name = "frmEllipses"
  70. Attribute VB_GlobalNameSpace = False
  71. Attribute VB_Creatable = False
  72. Attribute VB_PredeclaredId = True
  73. Attribute VB_Exposed = False
  74. Option Explicit
  75. ' Draw an ellipse using line segments.
  76. Private Sub EllipseWithSegments(ByVal obj As Object, ByVal xmin As Single, ByVal ymin As Single, ByVal xmax As Single, ByVal ymax As Single)
  77. Const PI = 3.14159265
  78. Dim theta As Single
  79. Dim cx As Single
  80. Dim cy As Single
  81. Dim radius_x As Single
  82. Dim radius_y As Single
  83. Dim X As Single
  84. Dim Y As Single
  85.     ' Find the center.
  86.     cx = (xmin + xmax) / 2
  87.     cy = (ymin + ymax) / 2
  88.     ' Find the X and Y half-widths.
  89.     radius_x = (xmax - xmin) / 2
  90.     radius_y = (ymax - ymin) / 2
  91.     ' Draw the ellipse.
  92.     obj.CurrentX = cx + radius_x
  93.     obj.CurrentY = cy
  94.     For theta = 0 To 2 * PI Step PI / 10
  95.         X = cx + radius_x * Cos(theta)
  96.         Y = cy + radius_y * Sin(theta)
  97.         obj.Line -(X, Y)
  98.     Next theta
  99.     obj.Line -(cx + radius_x, cy)
  100. End Sub
  101. ' Draw a circle stretched to obey the object's
  102. ' scale mode.
  103. Private Sub ScaledCircle(ByVal obj As Object, ByVal xmin As Single, ByVal ymin As Single, ByVal xmax As Single, ByVal ymax As Single)
  104. Dim cx As Single
  105. Dim cy As Single
  106. Dim wid As Single
  107. Dim hgt As Single
  108. Dim aspect As Single
  109. Dim radius As Single
  110.     ' Find the center.
  111.     cx = (xmin + xmax) / 2
  112.     cy = (ymin + ymax) / 2
  113.     ' Get the ellipse's size in twips.
  114.     wid = obj.ScaleX(xmax - xmin, obj.ScaleMode, vbTwips)
  115.     hgt = obj.ScaleY(ymax - ymin, obj.ScaleMode, vbTwips)
  116.     aspect = hgt / wid
  117.     ' See which dimension is larger.
  118.     If wid > hgt Then
  119.         ' The major axis is horizontal.
  120.         ' Get the radius in custom coordinates.
  121.         radius = obj.ScaleX(wid / 2, vbTwips, obj.ScaleMode)
  122.     Else
  123.         ' The major axis is vertical.
  124.         ' Get the radius in custom coordinates.
  125.         radius = aspect * obj.ScaleX(wid / 2, vbTwips, obj.ScaleMode)
  126.     End If
  127.     ' Draw the circle.
  128.     obj.Circle (cx, cy), radius, , , , aspect
  129. End Sub
  130. ' Define the custom scale modes and draw.
  131. Private Sub Form_Load()
  132.     ' Prepare the Circle picture.
  133.     picCircle.AutoRedraw = True
  134.     picCircle.Scale (0, 0)-(200, 100)
  135.     picCircle.Line (10, 10)-(90, 50), , B
  136.     picCircle.Circle (50, 30), 40
  137.     picCircle.Line (100, 10)-(190, 70), , B
  138.     picCircle.Circle (145, 40), 45
  139.     picCircle.Line (10, 75)-(190, 90), , B
  140.     picCircle.Circle (100, 82.5), 15
  141.     ' Prepare the line segments picture.
  142.     picLineSegments.AutoRedraw = True
  143.     picLineSegments.Scale (0, 0)-(200, 100)
  144.     picLineSegments.Line (10, 10)-(90, 50), , B
  145.     EllipseWithSegments picLineSegments, 10, 10, 90, 50
  146.     picLineSegments.Line (100, 10)-(190, 70), , B
  147.     EllipseWithSegments picLineSegments, 100, 10, 190, 70
  148.     picLineSegments.Line (10, 75)-(190, 90), , B
  149.     EllipseWithSegments picLineSegments, 10, 75, 190, 90
  150.     ' Prepare the ScaledCircle picture.
  151.     picScaledCircle.AutoRedraw = True
  152.     picScaledCircle.Scale (0, 0)-(200, 100)
  153.     picScaledCircle.Line (10, 10)-(90, 50), , B
  154.     ScaledCircle picScaledCircle, 10, 10, 90, 50
  155.     picScaledCircle.Line (100, 10)-(190, 70), , B
  156.     ScaledCircle picScaledCircle, 100, 10, 190, 70
  157.     picScaledCircle.Line (10, 75)-(190, 90), , B
  158.     ScaledCircle picScaledCircle, 10, 75, 190, 90
  159. End Sub
  160.